package com.xidian.zhiancloud.controller.myyb;

//import java.beans.PropertyDescriptor;
//import java.util.concurrent.TimeUnit;

import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.SysMenu;
import com.xidian.zhiancloud.model.SysUserAbandon;
import com.xidian.zhiancloud.service.myyb.ISysMenuService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 系统设置-系统菜单
 * *
 * @author
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends BaseController {

	protected Logger logger = LoggerFactory.getLogger(SysMenuController.class);
	
    @Autowired
    private ISysMenuService sysMenuService;

    @Autowired
    HttpServletRequest request;

    /**
     * 所有菜单列表
     * @param pid 名称等查询参数的集合
    * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/list")
    public ResultDTO<List<SysMenu>> list(@RequestParam(value="pid",required=false ,defaultValue="0") Integer pid) {
        //查询列表数据
        SysUserAbandon user =null;// getSessionUser();
        int roleid = user.getRoleId();
        List<SysMenu> menuList = new ArrayList<>();
        if(pid ==null ||pid==0){
            menuList = sysMenuService.queryList(null,roleid);
        }else{
            menuList = sysMenuService.queryList(pid,roleid);
        }
        return ResultDTO.successOf(menuList,GeneralResponseEnums.SUCCESS);
    }

    /**
     * 所有菜单列表
     */
    @RequestMapping("/queryAll")
    public ResultDTO queryAll(@RequestParam Map<String, Object> params) {
        //查询列表数据
        List<SysMenu> menuList = sysMenuService.queryList(params);
        return ResultDTO.successOf(menuList,GeneralResponseEnums.SUCCESS);
    }

    /**
     * 选择菜单(添加、修改菜单)
     */
    @RequestMapping("/select")
    public ResultDTO select() {
        //查询列表数据
        List<SysMenu> menuList = sysMenuService.queryNotButtonList();
        //添加顶级菜单
        SysMenu root = new SysMenu();
        root.setId(0);
        root.setName("一级菜单");
        root.setOpen(true);
        menuList.add(root);
		return ResultDTO.successOf(menuList,GeneralResponseEnums.SUCCESS);
    }


    /**
     * 菜单信息
     */
    @RequestMapping("/info/{menuId}")
    public ResultDTO info(@PathVariable("menuId") String menuId) {
        SysMenu menu = sysMenuService.queryObject(menuId);
		return ResultDTO.successOf(menu,GeneralResponseEnums.SUCCESS);
    }

    /**
     * 验证参数是否正确
     */
    private void verifyForm(SysMenu menu) {
        if (StringUtils.isBlank(menu.getName())) {
            throw new RuntimeException("菜单名称不能为空");
        }

        if (menu.getPid() == null) {
            throw new RuntimeException("上级菜单不能为空");
        }
        //菜单
       if (menu.getType() == 1) {
            if (StringUtils.isBlank(menu.getUrl())) {
                throw new RuntimeException("菜单URL不能为空");
            }
        }
        //上级菜单类型
        int parentType = 0;
        if (StringUtils.isNotEmpty(String.valueOf(menu.getPid())) && !"0".equals(String.valueOf(menu.getPid()))) {
            SysMenu parentMenu = sysMenuService.queryObject(String.valueOf(menu.getPid()));
            parentType = parentMenu.getType();
        }

        //目录、菜单
        if (menu.getType() == 0 ||
                menu.getType() == 1) {
            if (parentType != 0) {
                throw new RuntimeException("上级菜单只能为目录类型");
            }
            return;
        }

        //按钮
        if (menu.getType() == 2) {
            if (parentType != 1) {
                throw new RuntimeException("上级菜单只能为菜单类型");
            }
            return;
        }
    }

}
